package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.*;

public class BibliotecaApp {

    private static final String WELCOME_TO_BANGALORE_PUBLIC_LIBRARY = "Welcome to Bangalore Public Library";
    private UserMenu userMenu;
    private PrintStream printStream;
    private Collection<String> userMenuOptions = Arrays.asList("Enter 1 to view the list of all books.", "Enter 2 to view all movies.",
            "Enter 3 to check your membership details.", "Enter 4 to reserve a book.");
    private Map<String, Command> commandMap;
    private MemberRepository memberRepository;

    public BibliotecaApp(PrintStream ps, Map<String, Command> commandMap, UserMenu userMenu, MemberRepository memberRepository) {
        this.printStream = ps;
        this.commandMap = commandMap;
        this.userMenu = userMenu;
        this.memberRepository = memberRepository;
    }

    public static void main(String[] args) throws IOException {
        Map<String, Command> commandMap = new HashMap<String, Command>();
        commandMap.put("1", new PrintBooksCommand());
        commandMap.put("2", new PrintMoviesCommand());
        commandMap.put("3", new DisplayMembershipDetailsCommand());
        commandMap.put("4", new ReserveBookCommand());
        commandMap.put("Invalid", new InvalidOptionsCommand());
        PrintStream printer = System.out;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        UserMenu userMenu = new UserMenu(printer, reader);
        BibliotecaApp bibliotecaApp = new BibliotecaApp(printer, commandMap, userMenu, new MemberRepository(printer, reader));
        bibliotecaApp.startApp();
    }

    public void startApp() throws IOException {
        printWelcomeMessage();

        String userLoginPreference = userMenu.printLoginOptions();
        String loginStatus = memberRepository.login(userLoginPreference);
        String command;
        if(loginStatus.equals("Guest")){
            command = userMenu.printGuestMenuOptions();
        }else if (loginStatus.equals("Member")){
            command = userMenu.printUserMenuOptions();
        }else{
            command = "Invalid";
        }
        if (commandMap.keySet().contains(command)) {
            commandMap.get(command).execute();
        } else {
            commandMap.get("Invalid").execute();
        }
    }

    public void printWelcomeMessage(){
        printStream.println(WELCOME_TO_BANGALORE_PUBLIC_LIBRARY);
    }

}
